/**
 * @Filename: fileutil.cpp
 * @Description:
 * @Author: chengxiaojun
 * @Email: csforgood@gmail.com
 * @Time: 2011/11/11
 */
#include "fileutil.h"

bool isDir(const char* dir) {
	struct stat s;
	if (lstat(dir, &s) < 0) {
		return false;
	}

	if (!S_ISDIR(s.st_mode)) {
		return false;
	}

	return true;
}
bool isFile(const char* file) {
	struct stat s;
	if (lstat(file, &s) < 0) {
		return false;
	}

	if (!S_ISREG(s.st_mode) &&  !S_ISLNK(s.st_mode)) {
		return false;
	}

	return true;
}

void scanDir(const char* mainDir, vector<string> &filePath){

        if(!isDir(mainDir))  {
           return;
        }
        
        DIR *dir = opendir(mainDir);
	struct dirent *dt;
        char newDir[512] = {0};

	while ((dt = readdir(dir)) != NULL) {
		if (dt->d_name[0] == '.') {
			continue;
		}
		if (DT_DIR == dt->d_type) {
                        sprintf(newDir,"%s/%s",mainDir,dt->d_name);
			scanDir(newDir, filePath);
		} else if (DT_REG == dt->d_type) {

			int len = strlen(dt->d_name);
	                if(len>4) {
                              if(dt->d_name[len-1] == 'l' && dt->d_name[len-2] == 'm' && dt->d_name[len-3] == 'x' && dt->d_name[len-4] == '.' )  {
                                     sprintf(newDir,"%s/%s",mainDir,dt->d_name);
                                     filePath.push_back(string(newDir));
                              }
                        } 
                }
       }
}

